package com.itboye.zhujia.login;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.network.HttpRequest;
import com.itboye.zhujia.util.T;
import com.itboye.zhujia.volley.CodeErrorException;
import com.itboye.zhujia.volley.MyJsonRequest;
import com.itboye.zhujia.volley.XErrorListener;
import com.itboye.zhujia.volley.XRequestListener;
/**
 * 注册第2步（填写验证码）
 *
 */
public class RegisterActivityTwo extends BaseActivity implements OnClickListener{
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	TextView res_two_phone;//手机号
	EditText res_code;//验证码
	TextView res_two_time;//验证码重新发送
	TextView res_two_ok;//下一步
	
	private TimeCount time;//倒计时
	
	private Intent intent;
	
	private String phone;//接上页面传来的手机号

	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_register_two);
		
		add_shap_title_tv.setText("填写验证码");
		
		time = new TimeCount(60000, 1000, res_two_time);// 构造CountDownTimer对象
		time.start();
		
		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			
			break;
		case R.id.res_two_time://重新发送验证码
			getyzm();
			time.start();
			
			break;
		case R.id.res_two_ok://下一步
			VerificationYZM();
			
			break;

		default:
			break;
		}
		
	}
	/**
	 * 获取验证码
	 */
	private void getyzm() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", phone)
				.param("code_type", res_code.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(RegisterActivityTwo.this, response);
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(RegisterActivityTwo.this, msg);
						} else {
							T.showLong(RegisterActivityTwo.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	/**
	 * 验证验证码是否正确
	 */
	private void VerificationYZM() {
		if (res_code.getText().toString().trim() == null) {
			T.showLong(RegisterActivityTwo.this, "请输入验证码");
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_checkCode")
				.param("mobile", phone)
				.param("code", res_code.getText().toString())
				.param("code_type", "1")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(RegisterActivityTwo.this, response);
						Intent intent=new Intent(RegisterActivityTwo.this, RegisterActivityThree.class);
						intent.putExtra("phone", phone);
						startActivity(intent);
						
					}
				}).errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(RegisterActivityTwo.this, msg);
						} else {
							T.showLong(RegisterActivityTwo.this, exception.toString());
						}
						
					}
				}).build();
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
